const app = getApp()
var WXBizDataCrypt = require('../../utils/RdWXBizDataCrypt.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,

    userInfo: {},
    city: '',//存放地区
    station: '',//存放维修站
    perList: '',// 存放维修人员
  },
  getInfo: function(no) {
    var that = this
    app.req({
      url: 'User/Login',
      method: 'POST',
      data: {
        LoginNo: no,
        Password: '1',
      },
      success: function(res2) {
        if(res2.data.Code) {
          wx.setStorageSync('LoginNo', no)
          app.globalData.Authorization = res2.data.Result
          app.req({
            url:'User/CustomerWchatDetail',
            success:function(res3) {
              if(res3.data.Code) {
                that.setData({
                  userInfo:res3.data.Result
                })
                
              }
            }
          })
          that.setData({
            isLogin:true
          })
        }
      }
    })
  },

  // logout:function() {
  //   wx.removeStorageSync("LoginNo")
  //   app.globalData.Authorization = '',
  //   this.setData({
  //     isLogin:false,
  //     userInfo:{}
  //   })
  // },

  showtip:function() {
    var that = this
    wx.showModal({
      title: '是否退出登录?',
      concent:"是否退出登录",
      success: function (res) {
        // console.log(res);
        if (res.confirm) {
          wx.removeStorageSync("LoginNo")
          app.globalData.Authorization = '',
            that.setData({
              isLogin: false,
              userInfo: {}
            })
        } else {
          // console.log('取消')
        }
      }
    })

  },
  getPhoneNumber: function(e) {
    var that = this
    wx.login({
      success: function(data) {
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + app.globalData.appId + '&secret=' + app.globalData.sceret + '&js_code=' + data.code + '&grant_type=authorization_code',
          data:{},
          header:{
            "Content-Type":'json'
          },
          method:'GET',
          success:function(res) {
            var pc = new WXBizDataCrypt(app.globalData.appId, res.data.session_key)
            var data = pc.decryptData(e.detail.encryptedData, e.detail.iv)
            app.req({
              url:'User/CustomerWchat/IsExistence/' + data.phoneNumber,
              // method:"POST",
              data:{
                LoginNo:data.phoneNumber,
                Psaaword:'1'
              },
              success:function(res) {
                if (res.data.Code) {
                  app.req({
                    url:'User/CustomerWchatRegister',
                    method:'POST',
                    data:{
                      LoginNo:data.phoneNumber,
                      Password:'1'
                    },
                    success:function(res1) {
                      that.getInfo(data.phoneNumber)
                    }
                  })
                } else {
                  that.getInfo(data.phoneNumber)
                }
              }
          
            })
          },
          fail:function(res) {},
          complete:function(res) {}
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // app.checkLogin()
    if (wx.getStorageSync("LoginNo") && wx.getStorageSync("LoginNo") != null && wx.getStorageSync("LoginNo") != '') {
      this.getInfo(wx.getStorageSync("LoginNo"))
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})